package com.yhl.vocation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhl.vocation.entity.pojo.Admin;
import com.yhl.vocation.entity.pojo.Class;
import com.yhl.vocation.entity.pojo.Student;
import com.yhl.vocation.entity.pojo.Vacation;
import com.yhl.vocation.entity.vo.VacationVo;
import com.yhl.vocation.service.AdminService;
import com.yhl.vocation.service.ClassService;
import com.yhl.vocation.service.StudentService;
import com.yhl.vocation.service.VacationService;
import com.yhl.vocation.mapper.VacationMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
*
*/
@Service
public class VacationServiceImpl extends ServiceImpl<VacationMapper, Vacation>
implements VacationService{

    private StudentService studentService;
    private ClassService classService;
    private AdminService adminService;

    public VacationServiceImpl(StudentService studentService) {
        this.studentService = studentService;
    }

    @Override
    public boolean create(String openid, Date starts, Date ends, String type, String description) {
        Student student = studentService.getInfo(openid);
        Class classes = classService.getById(student.getClassId());
        Admin admin = adminService.getById(classes.getTeachId());
        if(student != null){
            Vacation vacation = new Vacation();
            vacation.setStart(starts);
            vacation.setEnd(ends);
            vacation.setDescription(description);
            vacation.setStatus((byte) 1);
            vacation.setStudentId(student.getId());
            vacation.setVocationWay(Byte.parseByte(type));
            vacation.setTeacherName(admin.getName());
            getBaseMapper().insert(vacation);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<Vacation> listByOpenId(String openid) {
        Student info = studentService.getInfo(openid);
        return getBaseMapper().selectList(new LambdaQueryWrapper<Vacation>().eq(Vacation::getStudentId, info.getId()).orderByDesc(Vacation::getStart));
    }

    @Override
    public boolean destory(Integer id, String openid, String description) {
        Vacation vacation = getBaseMapper().selectById(id);
        vacation.setReply(description);
        vacation.setStatus((byte) 3);
        getBaseMapper().updateById(vacation);
        return true;
    }

    @Override
    public List<VacationVo> listVocation(String id, String type) {
        return getBaseMapper().listVocation(id,type);
    }

    @Override
    public boolean updateVacation(String id, String type) {
        Vacation vacation = getBaseMapper().selectById(id);
        vacation.setStatus(Byte.parseByte(type));
        return getBaseMapper().updateById(vacation) > 0;
    }
}
